home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / Scripts / player.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  2.3 KB  |  123 lines

  1. #
  2. # The Python Imaging Library
  3. # $Id: player.py,v 1.1.1.1 1998/08/18 13:07:58 sjoerd Exp $
  4. #
  5.  
  6.  
  7. from Tkinter import *
  8. import Image, ImageTk
  9. import sys
  10.  
  11.  
  12. Image.DEBUG = 0
  13.  
  14.  
  15. # --------------------------------------------------------------------
  16. # experimental: support ARG animation scripts
  17.  
  18. import ArgImagePlugin
  19.  
  20. def applet_hook(animation, images):
  21.     app = animation(animation_display, images)
  22.     app.run()
  23.  
  24. ArgImagePlugin.APPLET_HOOK = applet_hook
  25.  
  26. class AppletDisplay:
  27.     def __init__(self, ui):
  28.     self.__ui = ui
  29.     def paste(self, im, bbox):
  30.     self.__ui.image.paste(im, bbox)
  31.     def update(self):
  32.     self.__ui.update_idletasks()
  33.  
  34. # --------------------------------------------------------------------
  35. # an image animation player
  36.  
  37. class UI(Label):
  38.  
  39.     def __init__(self, master, im):
  40.     if type(im) == type([]):
  41.         # list of images
  42.         self.im = im[1:]
  43.         im = self.im[0]
  44.     else:
  45.         # sequence
  46.         self.im = im
  47.  
  48.     if im.mode == "1":
  49.         self.image = ImageTk.BitmapImage(im, foreground="white")
  50.     else:
  51.         self.image = ImageTk.PhotoImage(im)
  52.  
  53.     # APPLET SUPPORT (very crude, and not 100% safe)
  54.     global animation_display
  55.     animation_display = AppletDisplay(self)
  56.  
  57.     Label.__init__(self, master, image=self.image, bg="black", bd=0)
  58.  
  59.     self.update()
  60.  
  61.     try:
  62.         duration = im.info["duration"]
  63.     except KeyError:
  64.         duration = 100
  65.     self.after(duration, self.next)
  66.  
  67.     def next(self):
  68.  
  69.     if type(self.im) == type([]):
  70.  
  71.         try:
  72.         im = self.im[0]
  73.         del self.im[0]
  74.         self.image.paste(im)
  75.         except IndexError:
  76.         return # end of list
  77.  
  78.     else:
  79.  
  80.         try:
  81.         im = self.im
  82.         im.seek(im.tell() + 1)
  83.         self.image.paste(im)
  84.         except EOFError:
  85.         return # end of file
  86.  
  87.     try:
  88.         duration = im.info["duration"]
  89.     except KeyError:
  90.         duration = 100
  91.     self.after(duration, self.next)
  92.  
  93.     self.update_idletasks()
  94.  
  95.  
  96. # --------------------------------------------------------------------
  97. # script interface
  98.  
  99. if __name__ == "__main__":
  100.  
  101.     if not sys.argv[1:]:
  102.     print "Syntax: python player.py imagefile(s)"
  103.     sys.exit(1)
  104.  
  105.     filename = sys.argv[1]
  106.  
  107.     root = Tk()
  108.     root.title(filename)
  109.  
  110.     if len(sys.argv) > 2:
  111.     # list of images
  112.     print "loading..."
  113.     im = []
  114.     for filename in sys.argv[1:]:
  115.         im.append(Image.open(filename))
  116.     else:
  117.     # sequence
  118.     im = Image.open(filename)
  119.  
  120.     UI(root, im).pack()
  121.  
  122.     root.mainloop()
  123.